#!/bin/bash

NEWROOTFS="./BareGenMoko"
TINDERBOX="./TinderGenMoko"
ADDPKGS="dhcpcd dropbear \
          iptables \
          libusb \
          mtd-utils \
          sqlite \
          usbutils \
          vim \
          wireless-tools wpa_supplicant"
EMRGCMD="emerge-armv4tl-softfloat-linux-gnueabi"
LOG="log.`echo ${0} | sed -e 's/\.\///'`"
DEFAULT_SERVICES="dropbear hald"
BOOT_SERVICES="net.eth0 net.usb0"
SYS_SERVICES="udev sysfs"
 

echo "Pruning ${NEWROOTFS}"
rm -rfv ${NEWROOTFS} > ${LOG}
 
echo "Setting up basic directories"
for i in boot etc dev proc root sys media/card media/card2; do
  mkdir -vp ${NEWROOTFS}/${i} >> ${LOG};
done;
mknod ${NEWROOTFS}/dev/null c 1 3
mknod ${NEWROOTFS}/dev/console c 5 1
 
echo "Setting up basic configuration"
for i in $( ls ${TINDERBOX}/etc/ | grep -iv portage ); do
  cp -vR ${TINDERBOX}/etc/${i} ${NEWROOTFS}/etc/ >> ${LOG}; 
done;
 
echo "Setting up portage configuration"
cp -LRv ${TINDERBOX}/etc/portage ${NEWROOTFS}/etc/ >> ${LOG}
sed -i -e 's/\(EMERGE_DEFAULT_OPTS=.*\)"$/\1 --getbinpkg"/' \
       -e 's/buildpkg//' \
       -e 's/INSTALL_MASK="/INSTALL_MASK="*.h /' \
         ${NEWROOTFS}/etc/make.conf
cp -Lv /etc/resolv.conf ${NEWROOTFS}/etc/ >> ${LOG}
 
echo "Updating remote binpkgs"
./sync-binpkgs >> ${LOG}
echo "Installing bare @system"
ROOT=${NEWROOTFS} USE="-X" ${EMRGCMD} -vg vim system >> ${LOG}
echo "adding my packages"
ROOT=${NEWROOTFS} USE="-X" ${EMRGCMD} -vg --noreplace ${ADDPKGS} >> ${LOG}

echo "Fixing GCC"
#for i in $( find /usr/lib* -name libgcc_s*.so* | grep armv4tl ); do
#  if [ -d `dirname ${i}` ] ; then
#    mkdir -p `dirname ${i}`
#  fi
#  echo "  "`echo ${i} | sed -e 's/.*\///'`;
#  cp -v ${i} ${NEWROOTFS}/`echo ${i} | sed -e 's/lib64/lib/'`;
#done;
#for i in $( find /usr/lib* -name libstdc++*.so* | grep armv4tl ); do
#  if [ -d `dirname ${i}` ] ; then
#    mkdir -p `dirname ${i}`
#  fi
#  echo "  "`echo ${i} | sed -e 's/.*\///'`;
#  cp -v ${i} ${NEWROOTFS}/`echo ${i} | sed -e 's/lib64/lib/'`;
#done;
cp -v /usr/lib64/gcc/armv4tl-softfloat-linux-gnueabi/4.3.3/libgcc_s.so.1 \
  ${NEWROOTFS}/usr/lib64/gcc/armv4tl-softfloat-linux-gnueabi/4.3.3/libgcc_s.so.1
ln -s ./libgcc_s.so.1 \
  ${NEWROOTFS}/usr/lib64/gcc/armv4tl-softfloat-linux-gnueabi/4.3.3/libgcc_s.so
cp -v /usr/lib64/gcc/armv4tl-softfloat-linux-gnueabi/4.3.3/libstdc++.so.6.0.10 \
  ${NEWROOTFS}/usr/lib64/gcc/armv4tl-softfloat-linux-gnueabi/4.3.3/libstdc++.so.6.0.10
ln -s ./libstdc++.so.6.0.10 \
  ${NEWROOTFS}/usr/lib64/gcc/armv4tl-softfloat-linux-gnueabi/4.3.3/libstdc++.so.6
ln -s ./libstdc++.so.6.0.10 \
  ${NEWROOTFS}/usr/lib64/gcc/armv4tl-softfloat-linux-gnueabi/4.3.3/libstdc++.so

echo "Installing base applications"
# hack(s) to install sturdy app(s)
EXTRA_ECONF="--enable-unique" ROOT=${NEWROOTFS} ${EMRGCMD} -v eix >> ${LOG}
 
echo "Installing custom files"
cp -Rv dingbat_config/* ${NEWROOTFS}/ >> ${LOG}
cp -Lv /etc/wpa_supplicant/wpa_supplicant.conf ${NEWROOTFS}/etc/wpa_supplicant/ >> ${LOG}
 
echo "Installing services"
ln -s ./net.lo ${NEWROOTFS}/etc/init.d/net.eth0
ln -s ./net.lo ${NEWROOTFS}/etc/init.d/net.usb0
for i in ${DEFAULT_SERVICES}; do
  ln -s /etc/init.d/${i} ${NEWROOTFS}/etc/runlevels/default/;
done;
for i in ${BOOT_SERVICES}; do
  ln -s /etc/init.d/${i} ${NEWROOTFS}/etc/runlevels/boot/;
done;
for i in ${SYS_SERVICES}; do
  ln -s /etc/init.d/${i} ${NEWROOTFS}/etc/runlevels/sysinit/;
done;
 
echo "Finding newest kernel"
# FIXME - for now we just dump what we find
echo "Loading modules"
tar -xzpvf AndyTracking/GTA02/modules-GTA02_andy-tracking-*.tar.gz -C ${NEWROOTFS}/ >> ${LOG}
 
echo "uncompressed size: `du -sh ${NEWROOTFS}/`"
 
echo "Creating image"
mkfs.jffs2 -r ${NEWROOTFS}/ --pad=0x700000 -o BareGenMoko.jffs2 -e 0x20000 -n
echo "compressed size: `du -b BareGenMoko.jffs2` Bytes"
echo "Done."
